/*
 * @Author: mikey.nibote 
 * @Date: 2018-09-06 16:34:45 
 * @Last Modified by: mikey.nibote
 * @Last Modified time: 2018-09-06 17:24:27
 */


module.exports = () => {
    return async function responseFormatter(ctx, next) {
        console.log(ctx.request.header)
        let JSONS = Object.assign({}, ctx.app.config.JSONS);
        await next();
        if (!ctx.body) {
            ctx.body = Object.assign(JSONS, {
                code: "404",
                message: "api不存在或地址错误"
            })
            ctx.status = 404;
        } else {
            ctx.status = ctx.body.code || 200;
            ctx.body = Object.assign(JSONS, {
                code: ctx.body.code || 200,
                data: ctx.body.data || [],
                message: ctx.body.message || 'success',
                uri: ctx.body.uri || 'http://127.0.0.1:7001',
                'timestamp': new Date() - 0
            })
        }
    }
}